package com.example.diary

import DiaryDbHelper
import android.app.Activity
import android.content.ContentValues
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import com.example.R

class DiaryEditActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_diary_edit)
        // 返回键
        val actionBar = supportActionBar
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true)
            actionBar.setDisplayHomeAsUpEnabled(true)
        }

        //为取消按钮和确认按钮设置点击事件
        val cancelButton: Button = findViewById(R.id.cancel_button)
        val confirmButton: Button = findViewById(R.id.confirm_button)
        cancelButton.setOnClickListener { cancelEdit(this) }
        confirmButton.setOnClickListener { confirmEdit(this) }
    }

    override fun onResume() {
        super.onResume()
        val id = intent.getIntExtra("id", 0)
        var title = "no title"
        var description = "nothing"

        // 在输入框内显示要修改的日记的标题和内容
        val dbHelper = DiaryDbHelper(this, "diary.db", 1)
        val db = dbHelper.writableDatabase
        val cursor = db.rawQuery("select * from diary where id = $id", null)
        if(cursor.moveToFirst()) {
            do {
                title = cursor.getString(cursor.getColumnIndexOrThrow("title"))
                description = cursor.getString(cursor.getColumnIndexOrThrow("description"))
            } while (cursor.moveToNext())
        }
        val titleEditText: EditText = findViewById(R.id.title_editText)
        val descriptionEditText: EditText = findViewById(R.id.description_editText)
        titleEditText.setText(title)
        descriptionEditText.setText(description)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            android.R.id.home -> {
                this.finish()
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }

    private fun confirmEdit(activity: Activity) {
        AlertDialog.Builder(this).apply {
            setTitle("修改日记")
            setMessage("您确定要保存修改吗？")
            setCancelable(false)
            setPositiveButton("确定") { _, _ ->
                val id = intent.getIntExtra("id", 0)
                val titleEditText: EditText = findViewById(R.id.title_editText)
                val descriptionEditText: EditText = findViewById(R.id.description_editText)
                val title = titleEditText.text.toString()
                val description = descriptionEditText.text.toString()

                // 修改当前日记到数据库
                val dbHelper = DiaryDbHelper(activity, "diary.db", 1)
                val db = dbHelper.writableDatabase
                val values = ContentValues().apply {
                    put("title", title)
                    put("description", description)
                }
                db.update("diary", values, "id = ?", arrayOf(id.toString()))
                Toast.makeText(activity, "日记修改成功", Toast.LENGTH_SHORT).show()
            }
            setNegativeButton("取消") { _, _ -> }
        }.show()
    }

    private fun cancelEdit(activity: Activity) {
        AlertDialog.Builder(this).apply {
            setTitle("取消修改")
            setMessage("您确定要取消修改吗？您作出的修改不会保存")
            setCancelable(false)
            setPositiveButton("确定") { _, _ ->
                activity.finish()
            }
            setNegativeButton("取消") { _, _ -> }
        }.show()
    }
}